package edu.odu.cs.sstore.client;

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ProfileController {

	@RequestMapping(value="/profile",method=RequestMethod.GET)
	public ModelAndView get(Locale locale, Model model) {
		return new ModelAndView("profile");
	}
	
	@RequestMapping(value="/profile",method=RequestMethod.POST)
	public ModelAndView post(
		@RequestParam("oldPassword") String oldPassword,
		@RequestParam("newPassword") String newPassword
	) {
		boolean success = true;
		String message = "Update successful.";
		
		// Ensure the passwords match and the passwords are not blank
		if(oldPassword.equals(newPassword) && !"".equals(newPassword)) {
			
			// TODO - Attempt to update the user's credentials
			// Catch any error and send to the server
			
		} else {
			success = false;
			if("".equals(newPassword))
				message = "ERROR: The password cannot be blank.";
			else
				message = "ERROR: Passwords do not match";
		}
		
		ModelAndView resultView = new ModelAndView("profile");
		resultView.addObject("message", message);
		resultView.addObject("success", success);
		return resultView;
	}
	
}
